package zgrjb.snbq.teach_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zgrjb.snbq.teach_platform.dto.model.AdviceDO;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.dto.req.AddAdviceRequest;
import zgrjb.snbq.teach_platform.dto.req.FeedbackAdviceRequest;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.mapper.AdviceMapper;
import zgrjb.snbq.teach_platform.service.AdviceService;
import zgrjb.snbq.teach_platform.util.IDUtil;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.util.Date;
import java.util.List;

@Service
public class AdviceServiceImpl implements AdviceService {

    @Autowired
    private AdviceMapper adviceMapper;


    @Override
    public JsonData addAdvice(AddAdviceRequest addAdviceRequest) {
        User user = LoginInterceptor.getCurrentUser();
        AdviceDO adviceDO = new AdviceDO();
        adviceDO.setClassId(addAdviceRequest.getClassId());
        adviceDO.setTeacherId(addAdviceRequest.getTeacherId());
        adviceDO.setMessage(addAdviceRequest.getMessage());
        adviceDO.setStudentId(user.getId());
        adviceDO.setId((Long) IDUtil.geneSnowFlakeID());
        adviceMapper.insert(adviceDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData list(Long classId) {
        User user = LoginInterceptor.getCurrentUser();
        List<AdviceDO> list = adviceMapper.selectList(new QueryWrapper<AdviceDO>()
                .eq("class_id",classId)
                .eq("student_id",user.getId()));
        return JsonData.buildSuccess(list);
    }

    @Override
    public JsonData listAll(Long classId) {
        User user = LoginInterceptor.getCurrentUser();
        List<AdviceDO> list = adviceMapper.selectList(new QueryWrapper<AdviceDO>()
                .eq("class_id",classId)
                .eq("teacher_id",user.getId()));
        return JsonData.buildSuccess(list);
    }

    @Override
    public JsonData feedbackAdvice(FeedbackAdviceRequest request) {
        adviceMapper.update(new UpdateWrapper<AdviceDO>()
                .eq("id",request.getAdviceId())
                .set("feedback",request.getFeedback())
                .set("feedback_time",new Date()));
        return JsonData.buildSuccess();
    }
}
